<?php

/**
 *    Desc
 *
 *    @author    Garbin
 *    @usage    none
 */
class FactoryApp extends MemberbaseApp
{
	var $_factory_mod;
	var $_type_mod;
    function __construct()
    {
        $this->FactoryApp();
    }
    function FactoryApp()
    {
        parent::__construct();
		$this->check_powers(USER_ADMIN);
		$this->_factory_mod =& m('factory');
		$this->_type_mod =& m('factorytype');
    }
	
    function index()
    {
		$page = $this->_get_page();
		$factories = $this->_factory_mod->find(array(
			'fields' => '*, factory.if_show',
			'join' => 'belongs_type',
			'order' => 'factory_id ASC',
			'count' => true,
			'limit' => $page['limit']
		));
		$page['item_count'] = $this->_factory_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('factories', $factories);
        $this->display('factory.index.html');
		$this->_curlocal(Lang::get('factory_manage'));
    }
	
	function type()
	{
		$types = $this->_type_mod->findAll();
		$this->assign('types', $types);
        $this->display('factory_type.index.html');
		$this->_curlocal(Lang::get('factory_manage'), 'index.php?app=factory', Lang::get('type_manage'));
	}
	
	function _assign_form()
	{
		$factory = array(
			'if_show' => 1,
		);
		$this->assign('factory', $factory);
	}
	
	function add()
	{
		if (!IS_POST) {
			$this->_assign_form();
			$this->_get_types();
			$memeber_mod=&m("member");
			$memeber_list = $memeber_mod->find(array(
					'fields' => 'user_id,user_name',
					'conditions'=>'role='.USER_QC,
					));
			$this->assign('member_list',$memeber_list);
			$this->assign('qf_list',array());
	        $this->display('factory.form.html');
			$this->_curlocal(Lang::get('factory_manage'), 'index.php?app=factory', Lang::get('add_new_factory'));
		} else {
			$factory_name = trim($_POST['factory_name']);
			$type_id = intval($_POST['type_id']);
			$address = trim($_POST['address']);
			$contact = trim($_POST['contact']);
			$qc = $_POST['qc'];
			$tel = trim($_POST['tel']);
			$fax = trim($_POST['fax']);
			$email = trim($_POST['email']);
			$description = trim($_POST['description']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($factory_name)) {
				$this->show_warning('factory_name_not_empty');
				return;
			}
			if (!$this->_factory_mod->unique($factory_name)) {
				$this->show_warning('factory_name_is_exist');
				return;
			}
			if (!$type_id) {
				$this->show_warning('factory_type_select_pls');
				return;
			}
			
			$data = array(
				'factory_name' => $factory_name,
				'type_id' => $type_id,
				'address' => $address,
				'contact' => $contact,
				'tel' => $tel,
				'fax' => $fax,
				'email' => $email,
				'description' => $description,
				'if_show' => $if_show,
			);
			
			
			
			$factory_id = $this->_factory_mod->add($data);
			if (!$factory_id) {
				$this->_error($this->_factory_mod->get_error());
				return;
			}
			$qc_factory_mod = & m("qcfactory");
			if(!empty($qc)){
				foreach($qc as $key=>$value){
					$qc_data = array(
							'user_id'=>$value,
							'factory_id'=>$factory_id
					);
					$qf_id = $qc_factory_mod->add($qc_data);
					if (!$qf_id) {
						$this->_error($qc_factory_mod->get_error());
						return;
					}
				}
			}
			$this->show_message('add_factory_success', Lang::get('factory'), 'index.php?app=factory');
		}
	}
		
	function edit()
	{
		$factory_id = intval($_GET['fid']);
		if (!$factory_id) {
			$this->show_warning('factory_not_exist');
			return;
		}
		$factory = $this->_factory_mod->get_info($factory_id);
		$qc_factory_mod = & m("qcfactory");
		$qf_list = $qc_factory_mod->find(array(
				   'fields'=>'user_id',
				   'conditions'=>'factory_id='.$factory_id,
				));
		$arr = array();
		if($qf_list){
			
			foreach($qf_list as $key=>$value){
				$arr[$key] = $value[user_id];
			}
			
		}
		$this->assign("qf_list",$arr);
		$memeber_mod=&m("member");
		$memeber_list = $memeber_mod->find(array(
				'fields' => 'user_id,user_name',
				'conditions'=>'role='.USER_QC,
		));
		$this->assign('member_list',$memeber_list);
		
		if (!$factory) {
			$this->show_warning('factory_not_exist');
			return;
		}
		if (!IS_POST) {
			$this->assign('factory', $factory);
			$this->_get_types();
			
			$this->display('factory.form.html');
			$this->_curlocal(Lang::get('factory_manage'), 'index.php?app=factory', Lang::get('edit_factory'));
		} else {
			$factory_name = trim($_POST['factory_name']);
			$type_id = intval($_POST['type_id']);
			$address = trim($_POST['address']);
			$contact = trim($_POST['contact']);
			$qc = $_POST['qc'];
			$tel = trim($_POST['tel']);
			$fax = trim($_POST['fax']);
			$email = trim($_POST['email']);
			$description = trim($_POST['description']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($factory_name)) {
				$this->show_warning('factory_name_not_empty');
				return;
			}
			if (!$this->_factory_mod->unique($factory_name, $factory_id)) {
				$this->show_warning('factory_name_is_exist');
				return;
			}
			if (!$type_id) {
				$this->show_warning('factory_type_select_pls');
				return;
			}
			
			$data = array(
				'factory_name' => $factory_name,
				'type_id' => $type_id,
				'address' => $address,
				'contact' => $contact,
				'tel' => $tel,
				'fax' => $fax,
				'email' => $email,
				'description' => $description,
				'if_show' => $if_show,
			);
			$this->_factory_mod->edit($factory_id, $data);
			if ($this->_factory_mod->get_error()) {
				$this->_error($this->_factory_mod->get_error());
				return;
			}
			$qc_factory_mod = & m("qcfactory");
			$qc_factory_mod->drop("factory_id={$factory_id}");
			if ($qc_factory_mod->get_error()) {
				$this->_error($qc_factory_mod->get_error());
				return;
			}
			if(!empty($qc)){
				foreach($qc as $key=>$value){
					$qc_data = array(
							'user_id'=>$value,
							'factory_id'=>$factory_id
					);
					$qf_id = $qc_factory_mod->add($qc_data);
					if (!$qf_id) {
						$this->_error($qc_factory_mod->get_error());
						return;
					}
				}
			}
			
			$order_mod =& m('order');
			$order_mod->edit('factory_id = ' . $factory_id, array('factory_name' => $factory_name));
			$this->show_message('edit_factory_success', Lang::get('factory'), 'index.php?app=factory');
		}
	}
	
	function del()
	{
		$factory_id = intval($_GET['fid']);
		if (!$factory_id) {
			$this->show_warning('factory_not_exist');
			return;
		}

		$this->_factory_mod->drop("factory_id={$factory_id}");
		if ($this->_factory_mod->get_error()) {
			$this->_error($this->_factory_mod->get_error());
			return;
		}
		$memeber_mod=&m("member");
		$memeber_mod->drop("factory_id={$factory_id}");
		if ($memeber_mod->get_error()) {
			$this->_error($memeber_mod->get_error());
			return;
		}
		
		$this->show_message('删除工厂成功', Lang::get('factory'), 'index.php?app=factory');
		
	}
	
	function _get_types()
	{
		$types = $this->_type_mod->find(array(
			'conditions' => 'if_show = 1'
		));
		$this->assign('types', $types);
	}
	
	function _assign_type_form()
	{
		$type = array(
			'if_show' => 1,
		);
		$this->assign('type', $type);
	}
	
	function add_type()
	{
		if (!IS_POST) {
			$this->_assign_type_form();
			
	        $this->display('factory_type.form.html');
			$this->_curlocal(Lang::get('factory_manage'), 'index.php?app=factory', Lang::get('type_manage'), 'index.php?app=factory&act=type', Lang::get('add_new_type'));
		} else {
			$type_name = trim($_POST['type_name']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($type_name)) {
				$this->show_warning('type_name_not_empty');
				return;
			}
			if (!$this->_type_mod->unique($type_name)) {
				$this->show_warning('type_name_is_exist');
				return;
			}
			
			$data = array(
				'type_name' => $type_name,
				'if_show' => $if_show
			);
			
			$type_id = $this->_type_mod->add($data);
			if (!$type_id) {
				$this->_error($this->_type_mod->get_error());
				return;
			}
			$this->show_message('add_type_success', Lang::get('type_manage'), 'index.php?app=factory&act=type');
		}
	}
	
	function edit_type()
	{
		$type_id = intval($_GET['type_id']);
		if (!$type_id) {
			$this->show_warning('type_not_exist');
			return;
		}
		$type = $this->_type_mod->get_info($type_id);
		if (!$type) {
			$this->show_warning('type_not_exist');
			return;
		}
		if (!IS_POST) {
			$this->assign('type', $type);
			
	        $this->display('factory_type.form.html');
			$this->_curlocal(Lang::get('factory_manage'), 'index.php?app=factory', Lang::get('type_manage'), 'index.php?app=factory&act=type', Lang::get('edit_type'));
		} else {
			$type_name = trim($_POST['type_name']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($type_name)) {
				$this->show_warning('type_name_not_empty');
				return;
			}
			if (!$this->_type_mod->unique($type_name, $type_id)) {
				$this->show_warning('type_name_is_exist');
				return;
			}
			
			$data = array(
				'type_name' => $type_name,
				'if_show' => $if_show
			);
			$this->_type_mod->edit($type_id, $data);
			if ($this->_type_mod->get_error()) {
				$this->_error($this->_type_mod->get_error());
				return;
			}
			$this->show_message('edit_type_success', Lang::get('type_manage'), 'index.php?app=factory&act=type');
		}
	}
	function check_factory()
	{
		$factory_name = empty($_GET['factory_name']) ? null : trim($_GET['factory_name']);
		$factory_id = empty($_GET['factory_id']) ? 0 : intval($_GET['factory_id']);
		if (!$factory_name) {
            echo ecm_json_encode(false);
            return;
		}
		echo ecm_json_encode($this->_factory_mod->unique($factory_name, $factory_id));
	}
	function check_type()
	{
		$type_name = empty($_GET['type_name']) ? null : trim($_GET['type_name']);
		$type_id = empty($_GET['type_id']) ? 0 : intval($_GET['type_id']);
		if (!$type_name) {
            echo ecm_json_encode(false);
            return;
		}
		echo ecm_json_encode($this->_type_mod->unique($type_name, $type_id));
	}
}

?>
